package other.mvvm.activity.src.app_package.viewpager


fun viewpagerMvvmAcitivityKt(
        applicationPackage: String?,
        activityClass: String,
        packageName: String,
        fragmentNames: ArrayList<String>
) = """
package ${packageName}
import android.os.Bundle
import cn.logicalthinking.mvvm.base.BaseActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import ${applicationPackage}.R
import ${packageName}.${activityClass}ViewModel
import ${applicationPackage}.databinding.Activity${activityClass}Binding
class ${activityClass}Activity : BaseActivity<Activity${activityClass}Binding, ${activityClass}ViewModel>() {
    //存放fragment 界面
    val fragments: MutableList<Fragment> = ArrayList()
    
    //fragmentAdapter
    var adapter: FragmentStateAdapter? = null

    override fun Activity${activityClass}Binding.initView() {
         ${ fragmentNames.forEach {
        "fragments.add(${it}Fragment()"
        } } }

    /**
     *  监听类
     */
    override fun initLiveDataObserve() {
    
        mViewModel.msgList.observe(this, Observer {
            if (mViewModel.page == 1) {
                adapter.setList(it)
            } else {
                adapter.addData(it)
            }

            if (adapter.data.size == 0) {
                adapter.setEmptyView(R.layout.layout_list_empty)
            }
        })
    
    }

    /**
     *  业务初始化
     */
    override fun initData() {
        initFragmentAdapter()
            
    }
    
    /**
     *  初始化fragments 布局
     */
    private fun initFragmentAdapter(){
        adapter = object : FragmentStateAdapter(this@DataActivity) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments.get(position)
            }
        }
        
        viewpager.adapter = adapter

        viewpager.offscreenPageLimit = ${fragmentNames.size}
        
        //与tablayout关联
        TabLayoutMediator(
            tabs,
            viewpager,
            true,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                tab.text = mViewModel.title[position])
            }).attach()
    }
   
    
} 
"""